package com.catal.framework.dao.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class MenuDaoImpl {

	public String funcFile = "src/conf/system.menu.xml";
	
	/**
	 * 菜单列表，每个菜单放于一个map.
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getAllMenus(){
		String[] files = funcFile.split(",");
		List<Map<String,Object>> menuList = new ArrayList<Map<String,Object>>();
		Map categorys = new HashMap();
		Map<String, Map> opers = new HashMap<String, Map>();
		for(String f : files){
			try {
				SAXReader reader = new SAXReader();
				Document document = reader.read(MenuDaoImpl.class.getResourceAsStream(f));
				Element root = document.getRootElement();
				//树型菜单名称
				List functions = root.elements("function");
				if(functions !=null){
					for(int i = 0; i < functions.size(); i++){
						Element e = (Element) functions.get(i);
						//menu--> {id:"",name:"",iconCls:"",pid:"",attributes:{url:"",version:"",order:"",operations:{}}}
						Map menu = new HashMap<String, Object>();
						Map attributes = new HashMap<String, String>();
						menu.put("attributes", attributes);
						String menuId = e.element("id").getTextTrim();
						menu.put("id", menuId);
						String menuName = e.element("name").getTextTrim();
						menu.put("name", menuName);
						String entry = e.element("entry").getTextTrim();
						attributes.put("url", entry);
						Element versionE = e.element("version");
						if(versionE != null){
							attributes.put("version", versionE.getTextTrim());
						}
						Element orderE = e.element("order");
						if(orderE!=null){
							attributes.put("order", orderE.getTextTrim());
						}
						Element categoryE = e.element("category");
						if(categoryE != null){
							String dirPath = categoryE.getTextTrim();
							handlerDir(categorys, menuList, dirPath);
							menu.put("pid", "category_" + dirPath);
						}
						Element iconClsE = e.element("iconCls");
						if(iconClsE != null){
							String iconCls = iconClsE.getTextTrim();
							menu.put("iconCls", iconCls);
						}
						Element operationsE = e.element("operations");
						if(operationsE != null){
							List operationList = operationsE.elements("operation");
							if(operationList!=null){
								//操作 (CRUD和其他操作)
								List<Map> tempOperList = new ArrayList<Map>();
								for(int k = 0; k<operationList.size(); k++){
									Element operE = (Element) operationList.get(i);
									// <operation>
									// 		<operId></operId> <!-- 全局Id -->
									// 		<shortId></shortId> <!-- 传到页面的id -->
									// 		<operName></operName><!-- 操作名称 -->
									// 		<operMethod></operMethod><!-- 操作方法 -->
									// </operation>
									String operId = menuId+"_" + operE.element("operId").getTextTrim();
									String shortId = operE.element("shortId").getTextTrim();
									String operName = operE.element("operName").getTextTrim();
									String operMethod = operE.element("operMethod").getTextTrim();
									Map<String, String> operMap = new HashMap<String, String>();
									operMap.put("operId", operId);
									operMap.put("shortId", shortId);
									operMap.put("operName", operName);
									operMap.put("operMethod", operMethod);
									//用操作方法名称作为操作的key
									opers.put(operMethod, operMap);
									tempOperList.add(operMap);
								}
								attributes.put("operations", tempOperList);
							}
						}
						menuList.add(menu);
					}
				}
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(menuList);
		return menuList;
	}
	
	/**
	 * 将目录路径分解成多级目录，例如：A/B/C/D分解成目录 A(名为A)，A/B(名为B)，A/B/C(名为C),A/B/C/D(名为D)
	 * @param dirsMap	目录容器
	 * @param path	目录路径
	 */
	@SuppressWarnings("unchecked")
	public static void handlerDir(Map dirsMap,  String path){
		if(dirsMap == null){
			dirsMap = new HashMap();
		}
		if(path == null || "".equals(path.trim())){
			return;
		}
		//目录是否已经存在
		if(dirsMap.get(path)!=null){
			return;
		}else{
			//判断是否多级目录
			String[] dirs = path.split("/",2);
			Map<String,String> dir = new HashMap<String, String>();
			if(dirs.length == 2){
				//"/"后面部分取出作为value 如A/B/C/D --> D 
				//然后把A/B/C再次进入递归处理
				dir.put("id", "category_"+path);
				dir.put("name", path.substring(path.lastIndexOf("/")+1));
				dir.put("pid", "category_"+path.substring(0,path.lastIndexOf("/")));
				dirsMap.put(path, dir);
				handlerDir(dirsMap, path.substring(0,path.lastIndexOf("/")));
			}else{
				dir.put("id", "category_"+path);
				dir.put("name", path);
				dirsMap.put(path, "category_"+dir);
			}
		}
	}
	
	/**
	 * 将目录路径分解成多级目录，例如：A/B/C/D分解成目录 A(名为A)，A/B(名为B)，A/B/C(名为C),A/B/C/D(名为D)
	 * @param dirsMap	目录容器
	 * @param menuList	菜单列表
	 * @param path	目录路径
	 */
	@SuppressWarnings("unchecked")
	public void handlerDir(Map dirsMap, List menuList, String path){
		if(dirsMap == null){
			dirsMap = new HashMap();
		}
		if(path == null || "".equals(path.trim())){
			return;
		}
		//目录是否已经存在
		if(dirsMap.get(path)!=null){
			return;
		}else{
			//判断是否多级目录
			String[] dirs = path.split("/",2);
			Map<String,String> dir = new HashMap<String, String>();
			if(dirs.length == 2){
				//"/"后面部分取出作为value 如A/B/C/D --> D 
				//然后把A/B/C再次进入递归处理
				dir.put("id", "category_"+path);
				dir.put("name", path.substring(path.lastIndexOf("/")+1));
				dir.put("pid", "category_"+path.substring(0,path.lastIndexOf("/")));
				//把目录放于目录容器和菜单列表
				dirsMap.put(path, dir);
				menuList.add(dir);
				handlerDir(dirsMap, menuList, path.substring(0,path.lastIndexOf("/")));
			}else{
				dir.put("id", "category_"+path);
				dir.put("name", path.substring(path.lastIndexOf("/")+1));
				//把目录放于目录容器和菜单列表
				dirsMap.put(path, dir);
				menuList.add(dir);
			}
		}
	}
	
	public static void main(String[] args) {
		Map m = new HashMap<String, String>();
		handlerDir(m, "A/B/C/D");
		handlerDir(m, "A/B/E/F");
		handlerDir(m, "B/B/B");
		handlerDir(m, "G");
		System.out.println(m.size());
		System.out.println(m);
		
		MenuDaoImpl menu = new MenuDaoImpl();
		List<Map<String, Object>> list = menu.getAllMenus();
		System.out.println(list.size());
		System.out.println(list);
	}

	public String getFuncFile() {
		return funcFile;
	}

	public void setFuncFile(String funcFile) {
		this.funcFile = funcFile;
	}
}
